package com.example.xianzhi.controller;

import cn.hutool.json.JSONUtil;
import com.example.xianzhi.entity.TbAd;
import com.example.xianzhi.service.AdService;
import com.example.xianzhi.service.impl.AdServiceImpl;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.MultipartFile;
import org.lanqiao.mvc.entity.YockMvcAnnotation;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@YockMvcAnnotation.Controller("/shuffling")
public class AdController {

    //轮播图业务层
    private AdService ad = new AdServiceImpl();

    /**
     * 跳转轮播图页面
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/toAd.do")
    @YockMvcAnnotation.ResponseDispatch("/WEB-INF/view/a.jsp")
    public ModelAndView toAd(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{
        ModelAndView model = new ModelAndView();

        return model;
    }


    /**
     * 轮播图片上传图片
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/shufflingupload.do")
    @YockMvcAnnotation.UploadFile
    @YockMvcAnnotation.ResponseBody
    public String shufflingupload(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "shuffling_pic") MultipartFile shuffling_pic
    ) {
        System.out.println("shuffling_pic is " + shuffling_pic);
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存图片名称的集合
        Map<String, Object> map = new HashMap<>();

//        ServletContext servletContext = request.getServletContext();
//        System.out.println("servletContext"+servletContext);

        //设置上传图片路径（访问路径）
        String uploadPath = request.getServletContext().getRealPath("upload");
        System.out.println(uploadPath);

        System.out.println("shuffling_pic.getName() is" + shuffling_pic.getName());


        if (shuffling_pic.getName()!=null){
            //获取图片上传名称（保证图片的唯一性）
            String fileName = System.currentTimeMillis() +  shuffling_pic.getName();
            System.out.println("fileName:"+fileName);
            // 上传图片
            shuffling_pic.transferTo(new File(uploadPath + File.separator + fileName));
            map.put("shuffling_pic",fileName);
        }



        //返回图片名称集
        String jsonStr = JSONUtil.toJsonStr(map);
        System.out.println(jsonStr);
        return jsonStr;
    }



    /**
     * 查询轮播图片
     * @param request
     * @param response
     * @return
     */
    @YockMvcAnnotation.RequestMapping("/selectAdpic.do")
    @YockMvcAnnotation.ResponseBody
    public String selectAdpic(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        //告诉前端返回来的是一个json数据
        response.setContentType("application/json;charset=utf-8");
        // 保存图片名称的集合
        Map<String, Object> map = new HashMap<>();
        //查询轮播图sql
        List<TbAd> selectshufflingpic = ad.selectshufflingpic();

        map.put("code",200);
        map.put("data",selectshufflingpic);

        String jsonStr = JSONUtil.toJsonStr(map);


        return jsonStr;
    }



    /**
     * 添加轮播图
     * @param request
     * @param response
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/addAd.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void addshuffling(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception{

        String ad_img = request.getParameter("ad_img");

        System.out.println("ad_img:" +ad_img);


        //添加轮播图
        ad.insertshuffling(ad_img);

    }

    /**
     * 删除轮播图
     * @param request
     * @param response
     * @throws Exception
     */
    @YockMvcAnnotation.RequestMapping("/deleteshuffling.do")	//配置二级路径
    @YockMvcAnnotation.ResponseVoid
    public void addtype(
            HttpServletRequest request,
            HttpServletResponse response,
            @YockMvcAnnotation.RequestParam(name = "ad_id") int ad_id
    ) throws Exception{

        ad.deleteshuffling(ad_id);



    }





}
